
//通告列表用的生成页面的一些方法
$(function() {
	GetCategory();
});
//绑定问题分类
function GetCategory(){
	QuesDwr.GetCategory(function(data){
		$("#kind").find("option").remove();
		$("#tkkind").find("option").remove();
		$("#pdkind").find("option").remove();
		$("#jdkind").find("option").remove();
		for(var i=0;i<data.length;i++){
	       $("#kind").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>"); 
	       $("#tkkind").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>"); 
	       $("#pdkind").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>"); 
	       $("#jdkind").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>"); 
		}
	});
}
//切换问题
function ChangeNav(cmd,obj){
	if($(obj).attr("class")=="onfocus"){
		return false;
	}
	$(".onfocus").addClass("losefocus");
	$(obj).removeClass("losefocus").addClass("onfocus");
	
	if(cmd=="xz"){
	  $("#tnav").css("background-image","url(img/gl/xzbg.jpg)");
	  $("#xxpanel").css("display","");
	  $("#tkpanel").css("display","none");
	  $("#pdpanel").css("display","none");
	  $("#jdpanel").css("display","none");
	  $("#drpanel").css("display","none");
	}else if(cmd=="tk"){
	  $("#tnav").css("background-image","url(img/gl/tkbg.jpg)");
	  $("#xxpanel").css("display","none");
	  $("#tkpanel").css("display","");
	  $("#pdpanel").css("display","none");
	  $("#jdpanel").css("display","none");
	  $("#drpanel").css("display","none");
	}else if(cmd=="pd"){
	  $("#tnav").css("background-image","url(img/gl/pdbg.jpg)");
	  $("#xxpanel").css("display","none");
	  $("#tkpanel").css("display","none");
	  $("#pdpanel").css("display","");
	  $("#jdpanel").css("display","none");
	  $("#drpanel").css("display","none");
	}else if(cmd=="jd"){
	  $("#tnav").css("background-image","url(img/gl/jdbg.jpg)");
	  $("#xxpanel").css("display","none");
	  $("#tkpanel").css("display","none");
	  $("#pdpanel").css("display","none");
	  $("#jdpanel").css("display","");
	  $("#drpanel").css("display","none");
	}else if(cmd=="dr"){
	  $("#tnav").css("background-image","url(img/gl/drbg.jpg)");
	  $("#xxpanel").css("display","none");
	  $("#tkpanel").css("display","none");
	  $("#pdpanel").css("display","none");
	  $("#jdpanel").css("display","none");
	  $("#drpanel").css("display","");
	} 
}
//添加选择
function AddXZ(){
   var cid=$("#kind").val();
   var problem=$("#problem").val();
   var xxa=$("#xxa").val();
   var xxb=$("#xxb").val();
   var xxc=$("#xxc").val();
   var xxd=$("#xxd").val();
   var level=$("#xxlevel").val();
   var $chkarry = $('input[type="checkbox"]');
   var ans="";
   var kind = $("#kind").val();
	if(kind.length<1){
		alert("请选择分类！");
		return false;
	}
   if(cid.length<1){
	 alert("请选择分类！");
     return false; 
   }
   if(problem.length<1){
	alert("问题不能为空！");
    return false;
   }
   if(xxa.length<1){
		alert("选项A不能为空！");
	    return false;
	   }
   if(xxb.length<1){
		alert("选项B不能为空！");
	    return false;
	   }
   if(xxc.length<1){
		alert("选项C不能为空！");
	    return false;
	   }
   if(!CheckAnswer(xxa,xxb,xxc,xxd)){
	   alert("选项不能重复！");
	    return false;
   }
   $chkarry.each(function() {
       var b = $(this).attr('checked');
       var v=$(this).attr('value');
       if(b==true){
    	   ans=ans+v+"|";
    	   if(v==3){
    		   if(xxc.length<1){
    			 alert("答案与选项不符！");
    		     return false;
    		   }
    	   }else if(v==4){
    		   if(xxd.length<1){
      			 alert("答案与选项不符！");
      		     return false;
      		   }
    	   }
       }
       v=null;
       b=null;
   });
   if(ans.length<1){
     alert("请选择答案！");
     return false;
   }
   QuesDwr.AddXZ(Number(cid),problem,xxa,xxb,xxc,xxd,Number(level),ans,function(data){
	   if(data>0){
	    alert("添加成功！");
	   }else if(data==-1){
		alert("该问题已经存在！");
	   }else{
		alert("添加失败！");
	   }
   });
   $("#problem").val("");
   $("#xxa").val("");
   $("#xxb").val("");
   $("#xxc").val("");
   $("#xxd").val("");
   $("#xxlevel").val("1");
   $chkarry.each(function() {
       $(this).attr('checked',false);
   });
   cid=null;
   problem=null;
   xxa=null;
   xxb=null;
   xxc=null;
   xxd=null;
   level=null;
   ans=null;
}
//添加填空
function AddTK(){
   var cid=$("#tkkind").val();
   var problem=$("#tkproblem").val();
   var ans=$("#tkans").val();
   var level=$("#tklevel").val();
    var kind = $("#tkkind").val();
	if(kind.length<1){
		alert("请选择分类！");
		return false;
	}
   if(cid.length<1){
		 alert("请选择分类！");
	     return false; 
	   }
   if(problem.length<1){
	alert("问题不能为空！");
    return false;
   }
   if(ans.length<1){
		alert("答案不能为空！");
	    return false;
	   }
   QuesDwr.AddTK(Number(cid),problem,Number(level),ans,function(data){
	   if(data>0){
		    alert("添加成功！");
		   }else if(data==-1){
			alert("该问题已经存在！");
		   }else{
			alert("添加失败！");
		   }
   });
   $("#tkproblem").val("");
   $("#tkans").val("");
   $("#tklevel").val("1");
   cid=null;
   problem=null;
   level=null;
   ans=null;
}
//添加判断
function AddPD(){
   var cid=$("#pdkind").val();
   var problem=$("#pdproblem").val();
   var ans=$("#pdans").val();
   var level=$("#pdlevel").val();
    var kind = $("#pdkind").val();
	if(kind.length<1){
		alert("请选择分类！");
		return false;
	}
   if(cid.length<1){
		 alert("请选择分类！");
	     return false; 
	   }
   if(problem.length<1){
	alert("问题不能为空！");
    return false;
   }
   if(ans.length<1){
		alert("答案不能为空！");
	    return false;
	   }
   QuesDwr.AddPD(Number(cid),problem,Number(level),ans,function(data){
	   if(data>0){
		    alert("添加成功！");
		   }else if(data==-1){
			alert("该问题已经存在！");
		   }else{
			alert("添加失败！");
		   }
   });
   $("#pdproblem").val("");
   $("#pdans").val("1");
   $("#pdlevel").val("1");
   cid=null;
   problem=null;
   level=null;
   ans=null;
}
//添加简答
function AddJD(){
   var cid=$("#jdkind").val();
   var problem=$("#jdproblem").val();
   var ans=$("#jdans").val();
   var level=$("#jdlevel").val();
    var kind = $("#jdkind").val();
	if(kind.length<1){
		alert("请选择分类！");
		return false;
	}
   if(cid.length<1){
		 alert("请选择分类！");
	     return false; 
	   }
   if(problem.length<1){
	alert("问题不能为空！");
    return false;
   }
   if(ans.length<1){
		alert("答案不能为空！");
	    return false;
	   }
   QuesDwr.AddJD(Number(cid),problem,Number(level),ans,function(data){
	   if(data>0){
		    alert("添加成功！");
		   }else if(data==-1){
			alert("该问题已经存在！");
		   }else{
			alert("添加失败！");
		   }
   });
   $("#jdproblem").val("");
   $("#jdans").val("");
   $("#jdlevel").val("1");
   cid=null;
   problem=null;
   level=null;
   ans=null;
}
/// <summary>
/// 检查是否有重复选项
/// </summary>
/// <returns></returns>
function CheckAnswer(answ1,answ2,answ3,answ4)
{
    var count = 0;
    var isOk = true;
    if (answ1.length > 0)
    {
        count++;
    }
    if (answ2.length > 0)
    {
        count++;
    }
    if (answ3.length > 0)
    {
        count++;
    }
    if (answ4.length > 0)
    {
        count++;
    }
    if (count == 2)
    {
        if (answ1 == answ2)
        {
            isOk = false;
        }
    }
    if (count == 3)
    {
        if ((answ1 == answ2) || (answ1 == answ3) || (answ2 == answ3))
        {
            isOk = false;
        }
    }
    if (count == 4)
    {
        if ((answ1 == answ2) || (answ1 == answ3) || (answ1 == answ4) || (answ2 == answ3) || (answ2 == answ4) || (answ4 == answ3))
        {
            isOk = false;
        }
    }
    return isOk;
}
//导入问题
function ImportQues(){
	var imgfile=$("#excel").val();
	if(imgfile.length>4){
		
	    if(imgfile.lastIndexOf(".")==-1)
	    {
	         alert("只允许上次Excel文件！");
       		 return false;
	    }
	    var ext=imgfile.substring(imgfile.lastIndexOf(".")+1).toLowerCase();
		if(ext!="xls") {
        	 alert("只允许上次Excel文件！");
	       		return false;
         }
	 }else{
		alert("请选择要上传Excel！");	
		return false;
	}
	$("#addQues").submit();
}